package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.zzyl.common.thread.UserThreadLocal;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.PageBean;
import com.zzyl.nursing.dto.NursingProjectDto;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 预约信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-02
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private INursingProjectService nursingProjectService;
    @Override
    public List<NursingProject> findByPage(NursingProjectDto dto) {
        LambdaQueryWrapper<NursingProject> queryWrapper = Wrappers.<NursingProject>lambdaQuery();
        String name = dto.getName();
        Integer status = dto.getStatus();
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like(NursingProject::getName, name);
        }
        if (status != null) {
            queryWrapper.eq(NursingProject::getStatus, status);
        }
        return nursingProjectService.list(queryWrapper);
    }
    @Override
    public NursingProject findById(Long id) {
        return nursingProjectService.getById(id);
    }
    @Override
    public PageBean<Reservation> findPage(NursingProjectDto dto) {
        LambdaQueryWrapper<Reservation> queryWrapper = Wrappers.lambdaQuery();
        Integer status = dto.getStatus();
        if (status != null) {
            queryWrapper.eq(Reservation::getStatus, status);
        }
        List<Reservation> list = this.list(queryWrapper);
        PageBean<Reservation> pageBean = new PageBean<>();
        PageInfo<Reservation> page = PageInfo.of(list);
        pageBean.setTotal(page.getTotal());
        pageBean.setRows(list);
        return pageBean;
    }

    @Override
    public Integer getCountById() {
        Long userId = UserThreadLocal.getUserId();
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        return reservationMapper.getCountById(userId, startTime, endTime);
    }
}
